package com.lora.scheduled;

import com.lora.communication.socket.ActiveDoor;
import com.lora.communication.socket.SocketCmdInstance;
import com.lora.entity.EntranceGuard;
import com.lora.enums.DoorEnum;
import com.lora.enums.EquipmentEnum;
import com.lora.service.EntranceGuardService;
import com.lora.service.EquipmentService;
import com.lora.util.ApplicationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Configuration
@EnableScheduling
public class EntranceGuardActive {

    Logger logger= LoggerFactory.getLogger(EntranceGuardActive.class);

    @Autowired
    private SocketCmdInstance socketCmdInstance;
    @Autowired
    EntranceGuardService entranceGuardService;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private EquipmentService equipmentService;

   /* @Scheduled(fixedRate=86400000)
    private void activeDoor() {
        System.out.println("门禁心跳监测已开启");
        ActiveDoor activeDoor= ApplicationUtil.getBean(ActiveDoor.class);
        activeDoor.activeDoor();
    }*/

    /*private void camera() {
        System.out.println("摄像头心跳监测已开启");
        ActiveDoor activeDoor= ApplicationUtil.getBean(ActiveDoor.class);
        activeDoor.activeCamera();
    }*/

    @Scheduled(fixedRate=600000)
    public void checkDoorInfo(){
        Map<String,Object> map=new HashMap<>();
        map.put("type", EquipmentEnum.ACCSS_CONTROL.getValue());
        List<Map<String, Object>> query = equipmentService.query(map);
        if(query!=null&&query.size()>0) {
            for (Map<String, Object> stringObjectMap : query) {
                String position = stringObjectMap.get("parameter").toString();
                save(stringObjectMap.get("eid").toString(), position.substring(2), 60000);
            }
        }
    }

    private void save(String eit,String url,int port){
        Map map=new HashMap<>();
        List<Map<String, Object>> maps = jdbcTemplate.queryForList("select email,username from sys_user");
        Map<String,String> namemap=new HashMap<>();
        if(maps!=null&&maps.size()>0){
            for (Map<String, Object> stringObjectMap : maps) {
                if(stringObjectMap.get("email")!=null&&!stringObjectMap.get("email").equals(""))
                    namemap.put(stringObjectMap.get("email").toString().trim(),stringObjectMap.get("username").toString());
            }
        }
        List<Map<String, Object>> maxtime = jdbcTemplate.queryForList("select max(time) as time from entrance_guard");
        String logs = socketCmdInstance.getLastRecord(eit, url, port);
        if(logs!=null){
            String substring = logs.substring(16);
            String rno = substring.substring(0, 8);
            rno=rno.substring(2)+rno.substring(0,2);
            int ReasonNo = Integer.parseInt(rno, 16);
            substring=substring.substring(10);
            String state = Integer.parseInt(substring.substring(0, 2),16)==0?"不通过":"通过";
            String address = Integer.parseInt(substring.substring(2, 4),16)==1?"玻璃门":"机房大门";
            String way=Integer.parseInt(substring.substring(4,6),16)==1?"进门":"出门";
            String carnum = substring.substring(6, 14);
            carnum=carnum.substring(2)+carnum.substring(0,2);
            carnum=Long.parseLong(carnum,16)+"";
            substring=substring.substring(14);
            String time=substring.substring(0,4)+"年"+substring.substring(4,6)+"月"+substring.substring(6,8)+"日 "+substring.substring(8,10)+":"+substring.substring(10,12)+":"+substring.substring(12,14);
            String remark= DoorEnum.get(substring.substring(14,16));
            EntranceGuard entranceGuard=new EntranceGuard();
            entranceGuard.setState(state);
            entranceGuard.setTime(time);
            if(namemap.get(carnum.trim())!=null){
                entranceGuard.setName(namemap.get(carnum.trim()));
            }else{
                entranceGuard.setName("N"+carnum);
            }
            entranceGuard.setCardnum(carnum);
            entranceGuard.setReasonno(ReasonNo+"");
            entranceGuard.setWay(way);
            entranceGuard.setAddress(address);
            entranceGuard.setDepartment(remark);
            entranceGuard.setSn(eit);
            if(maxtime!=null&&maxtime.size()>0
                    &&maxtime.get(0)!=null&&
                    maxtime.get(0).get("time") !=null&&!
                    maxtime.get(0).get("time").toString().trim().equals("")){
                if (maxtime.get(0).get("time").toString().trim().equals(entranceGuard.getTime().trim())) {
                    return;
                }
            }
            entranceGuardService.save(entranceGuard);
            logger.info("门禁最新数据已缓存");
        }
    }
}
